/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package glz.hawk.springboot.jooq.poc;

import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.*;
import org.jooq.meta.jaxb.Configuration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * This class is responsible for
 *
 * @author Zhang Peng
 */
@SpringBootApplication
public class H2GeneratorApplication {

    public static void main(String[] args) throws Exception {

        SpringApplication app = new SpringApplication(H2GeneratorApplication.class);
        //  先初始化H2数据库
        app.setWebApplicationType(WebApplicationType.NONE);
        ConfigurableApplicationContext context = app.run(args);

        // 生成代码
        Configuration configuration = new Configuration()
            .withJdbc(new Jdbc()
                .withDriver("org.h2.Driver")
                .withUrl("jdbc:h2:mem:library;DB_CLOSE_DELAY=-1")
                .withUser("sa")
                .withPassword(""))
            .withGenerator(new Generator()
                .withName("org.jooq.codegen.JavaGenerator")
                .withDatabase(new Database()
                    .withName("org.jooq.meta.h2.H2Database")
                    .withIncludes(".*")      // 包含所有表
                    .withExcludes("")        // 排除规则
                    .withInputSchema("PUBLIC") // H2 默认模式是 PUBLIC
                    // 添加 forcedType 配置, 没起作用
                    .withForcedTypes(
                        new ForcedType()
                            .withName("CLOB") // 映射为 CLOB 类型
                            .withIncludeTypes("VARCHAR|TEXT|LONGVARCHAR|STRING") // 匹配的数据库类型
                            .withIncludeExpression("length > 10000")// 长度超过 10000 的字段
                    )
                    .withProperties(
                        new Property().withKey("defaultNameCase").withValue("as_is")))
                .withGenerate(new Generate()
                    .withDaos(true)          // 生成 DAO 类
                    .withPojos(true)         // 生成 POJO 类
                    .withInterfaces(true)    // 生成接口
                    .withFluentSetters(true) // 使用流式 setter
                    .withJavaTimeTypes(true) // 使用 Java 8 时间类型
                )
                .withTarget(new Target()
                    .withPackageName("com.example.h2")  // 生成的包名
                    .withDirectory("springboot/springboot-jooq/src/generated/java")));    // 输出目录

        // 运行代码生成器
        GenerationTool.generate(configuration);

        context.close();
    }
}
